﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using FWMS.COMMAND;
using System.IO;
using System.Runtime.InteropServices;

namespace FWMS
{
    public partial class Form_cukumingxi : Form
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        public static extern void fclose(IntPtr hObject);
        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);
        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
        //EXCEL明细
        private static string sheet4name = "insert";

        private bool isLoad = true;
        private bool isTiaoWeiLiaoLoad = true;
        private bool isFuLiaoLoad = true;
        private bool isChengPinLoad = true;

        private DateTime  yuanliaoLastJijiDate;
        private DateTime tiaoliaoLastJijiDate;
        private DateTime fuliaoLastJijiDate;
        private DateTime chengpinLastJijiDate;
        //查询结果的数据
        public object[,] Selectresult;

        public Form_cukumingxi()
        {
            InitializeComponent();
        }

        private void Form_cukumingxi_Load(object sender, EventArgs e)
        {
            initYuanLiao();
            
        }

        private Object getData(int tabIndex)
        {
            if (tabIndex == 0)
            {
                Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();

                bean = ChukumingxiService.chukuguanli_ServiceSelect(this.dateTimePicker_startday.Value, this.dateTimePicker_endday.Value,0);
                if (bean.Selectresult == null)
                {
                    return null;
                }
                else
                {
                    return bean;
                }
            }
            else if (tabIndex == 1)
            {
                Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();

                bean = ChukumingxiService.chukuguanli_ServiceSelect(this.tiaoliao_startday.Value, this.tiaoliao_endday.Value,1);
                if (bean.Selectresult == null)
                {
                    return null;
                }
                else
                {
                    return bean;
                }
            }
            else if (tabIndex == 2)
            {
                Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();

                bean = ChukumingxiService.chukuguanli_ServiceSelect(this.fuliao_startday.Value, this.fuliao_endDay.Value, 2);
                if (bean.Selectresult == null)
                {
                    return null;
                }
                else
                {
                    return bean;
                }
            }
           
            return null;
           
        }

        //接收数据并显示
        private void datagridviewadddata(object bean, int tabIndex,bool isSelectByPeriod)
        {

            string guige = "";
            float shijiTotal = 0;
            if (tabIndex == 0)
            {
                this.dataGridView_yuanliao.AllowUserToAddRows = false;
                Bean.Chukuguanli_yuancailiaoBean yuancailiaobean = (Bean.Chukuguanli_yuancailiaoBean)bean;
                this.btn_detail.UseColumnTextForButtonValue = true;
                this.dataGridView_yuanliao.Rows.Clear();
                string beforePinming = "";
                string beforeGuige = "";
                int count = 0;
                for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                {
                    this.dataGridView_yuanliao.Rows.Add();
                    //品名
                    this.dataGridView_yuanliao.Rows[j].Cells[0].Value = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                    //规格
                    this.dataGridView_yuanliao.Rows[j].Cells[1].Value = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                    //备注
                    this.dataGridView_yuanliao.Rows[j].Cells[2].Value = yuancailiaobean.Selectresult[j, 2].ToString().Trim();
                    this.dataGridView_yuanliao.Rows[j].Cells[3].Value = yuancailiaobean.Selectresult[j, 3].ToString().Trim();
                    this.dataGridView_yuanliao.Rows[j].Cells[6].Value = yuancailiaobean.Selectresult[j, 4].ToString().Trim();
                    if (isSelectByPeriod)
                    {
                        this.dataGridView_yuanliao.Rows[j].Cells[7].Value = yuancailiaobean.Selectresult[j, 5].ToString().Trim();
                        this.dataGridView_yuanliao.Rows[j].Cells[5].Value = yuancailiaobean.Selectresult[j, 6].ToString().Trim();
                        this.dataGridView_yuanliao.Rows[j].DefaultCellStyle.BackColor = Color.LightGray;
                    }
                    else
                    {
                        Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
                        //取得实际重量
                        string shijizhongliang = service.chukushijiZhongliang(dateTimePicker_startday.Value.ToString("yyyyMMdd"), dateTimePicker_endday.Value.ToString("yyyyMMdd"), yuancailiaobean.Selectresult[j, 0].ToString().Trim(), yuancailiaobean.Selectresult[j, 1].ToString().Trim(), yuancailiaobean.Selectresult[j, 2].ToString().Trim(), "0");

                        if (Command.nullToSpace(shijizhongliang).Equals(""))
                        {
                            this.dataGridView_yuanliao.Rows[j].Cells[7].Value = yuancailiaobean.Selectresult[j, 4].ToString().Trim();
                        }
                        else
                        {
                            this.dataGridView_yuanliao.Rows[j].Cells[7].Value = shijizhongliang;
                        }
                        this.dataGridView_yuanliao.Rows[j].Cells[5].Value = yuancailiaobean.Selectresult[j, 5].ToString().Trim(); ;
                    }

                    this.btn_detail.Text = "查看详细";
                    
                }
                float zhongliang = 0;
                if (!isSelectByPeriod)
                {
                    
                    for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                    {
                        //品名，规格不一致时添加新行
                        if (!"".Equals(beforePinming) && (!beforePinming.Equals(yuancailiaobean.Selectresult[j, 0].ToString().Trim()) || !beforeGuige.Equals(yuancailiaobean.Selectresult[j, 1].ToString().Trim())))
                        {
                            DataGridViewRow row = new DataGridViewRow();
                            row.DefaultCellStyle.BackColor = Color.White;
                            this.dataGridView_yuanliao.Rows.InsertRange(count, row);
                            this.dataGridView_yuanliao.Rows[count].Cells[7].Style.BackColor = Color.LightCyan;
                            this.dataGridView_yuanliao.Rows[count].Cells[7].Value = Convert.ToString(zhongliang);
                            this.dataGridView_yuanliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_yuanliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_yuanliao.Rows[count - 1].Cells[1].Value).ToString();
                            this.dataGridView_yuanliao.Rows[count].Cells[6].Value = Convert.ToString(shijiTotal);
                            count++;
                            shijiTotal = 0;
                            zhongliang = 0;
                        }

                        beforePinming = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                        beforeGuige = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                        shijiTotal += float.Parse(yuancailiaobean.Selectresult[j, 4].ToString().Trim());
                        zhongliang += float.Parse(this.dataGridView_yuanliao.Rows[count].Cells[7].Value.ToString());
                        count++;

                    }

                    //最后一条

                    DataGridViewRow rowLast = new DataGridViewRow();
                    rowLast.DefaultCellStyle.BackColor = Color.White;
                    this.dataGridView_yuanliao.Rows.Add(rowLast);
                    this.dataGridView_yuanliao.Rows[count].Cells[7].Style.BackColor = Color.LightCyan;
                    this.dataGridView_yuanliao.Rows[count].Cells[7].Value = Convert.ToString(zhongliang);
                    this.dataGridView_yuanliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_yuanliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_yuanliao.Rows[count - 1].Cells[1].Value).ToString();
                    this.dataGridView_yuanliao.Rows[count].Cells[6].Value = Convert.ToString(shijiTotal);
                }
            }
            else if (tabIndex == 1)
            {
                this.dataGridView_tiaoliao.AllowUserToAddRows = false;
                Bean.Chukuguanli_yuancailiaoBean yuancailiaobean = (Bean.Chukuguanli_yuancailiaoBean)bean;
                this.tiaoliao_details.UseColumnTextForButtonValue = true;
                this.dataGridView_tiaoliao.Rows.Clear();
                string beforePinming = "";
                string beforeGuige = "";
                int count = 0;
                for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                {
                    this.dataGridView_tiaoliao.Rows.Add();
                    this.dataGridView_tiaoliao.Rows[j].Cells[0].Value = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                    this.dataGridView_tiaoliao.Rows[j].Cells[1].Value = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                    this.dataGridView_tiaoliao.Rows[j].Cells[2].Value = yuancailiaobean.Selectresult[j, 2].ToString().Trim();
                    this.dataGridView_tiaoliao.Rows[j].Cells[3].Value = yuancailiaobean.Selectresult[j, 3].ToString().Trim();
                    this.dataGridView_tiaoliao.Rows[j].Cells[6].Value = yuancailiaobean.Selectresult[j, 4].ToString().Trim();
                    if (isSelectByPeriod)
                    {
                        this.dataGridView_tiaoliao.Rows[j].Cells[7].Value = yuancailiaobean.Selectresult[j, 5].ToString().Trim();
                        this.dataGridView_tiaoliao.Rows[j].Cells[5].Value = yuancailiaobean.Selectresult[j, 6].ToString().Trim();
                        this.dataGridView_tiaoliao.Rows[j].DefaultCellStyle.BackColor = Color.LightGray;
                    }
                    else
                    {
                        Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
                        //取得实际重量
                        string shijizhongliang = service.chukushijiZhongliang(tiaoliao_startday.Value.ToString("yyyyMMdd"), tiaoliao_endday.Value.ToString("yyyyMMdd"), yuancailiaobean.Selectresult[j, 0].ToString().Trim(), yuancailiaobean.Selectresult[j, 1].ToString().Trim(), yuancailiaobean.Selectresult[j, 2].ToString().Trim(), "1");

                        if (Command.nullToSpace(shijizhongliang).Equals(""))
                        {
                            this.dataGridView_tiaoliao.Rows[j].Cells[7].Value = yuancailiaobean.Selectresult[j, 4].ToString().Trim();
                        }
                        else
                        {
                            this.dataGridView_tiaoliao.Rows[j].Cells[7].Value = shijizhongliang;
                        }
                    }
                    this.tiaoliao_details.Text = "查看详细";

                }
                float zhongliang = 0;
                if (!isSelectByPeriod)
                {
                    for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                    {
                        //品名，规格不一致时添加新行
                        if (!"".Equals(beforePinming) && (!beforePinming.Equals(yuancailiaobean.Selectresult[j, 0].ToString().Trim()) || !beforeGuige.Equals(yuancailiaobean.Selectresult[j, 1].ToString().Trim())))
                        {
                            DataGridViewRow row = new DataGridViewRow();
                            row.DefaultCellStyle.BackColor = Color.White;
                            this.dataGridView_tiaoliao.Rows.InsertRange(count, row);
                            this.dataGridView_tiaoliao.Rows[count].Cells[7].Style.BackColor = Color.LightCyan;
                            this.dataGridView_tiaoliao.Rows[count].Cells[7].Value = Convert.ToString(zhongliang);
                            this.dataGridView_tiaoliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_tiaoliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_tiaoliao.Rows[count - 1].Cells[1].Value).ToString();
                            this.dataGridView_tiaoliao.Rows[count].Cells[6].Value = Convert.ToString(shijiTotal);
                            count++;
                            shijiTotal = 0;
                            zhongliang = 0;
                        }

                        beforePinming = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                        beforeGuige = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                        shijiTotal += float.Parse(yuancailiaobean.Selectresult[j, 4].ToString().Trim());
                        zhongliang += float.Parse(this.dataGridView_tiaoliao.Rows[count].Cells[7].Value.ToString());
                        count++;

                    }

                    //最后一条

                    DataGridViewRow rowLast = new DataGridViewRow();
                    rowLast.DefaultCellStyle.BackColor = Color.White;
                    this.dataGridView_tiaoliao.Rows.Add(rowLast);
                    this.dataGridView_tiaoliao.Rows[count].Cells[7].Style.BackColor = Color.LightCyan;
                    this.dataGridView_tiaoliao.Rows[count].Cells[7].Value = Convert.ToString(zhongliang);
                    this.dataGridView_tiaoliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_tiaoliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_tiaoliao.Rows[count - 1].Cells[1].Value).ToString();
                    this.dataGridView_tiaoliao.Rows[count].Cells[6].Value = Convert.ToString(shijiTotal);
                }
                }
            else if (tabIndex == 2)
            {
                this.dataGridView_fuliao.AllowUserToAddRows = false;
                Bean.Chukuguanli_yuancailiaoBean yuancailiaobean = (Bean.Chukuguanli_yuancailiaoBean)bean;
                this.fuliao_details.UseColumnTextForButtonValue = true;
                this.dataGridView_fuliao.Rows.Clear();
                string beforePinming = "";
                string beforeGuige = "";
                int count = 0;
                for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                {
                    this.dataGridView_fuliao.Rows.Add();
                    this.dataGridView_fuliao.Rows[j].Cells[0].Value = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                    this.dataGridView_fuliao.Rows[j].Cells[1].Value = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                    this.dataGridView_fuliao.Rows[j].Cells[2].Value = yuancailiaobean.Selectresult[j, 2].ToString().Trim();
                    this.dataGridView_fuliao.Rows[j].Cells[3].Value = yuancailiaobean.Selectresult[j, 3].ToString().Trim();
                    this.dataGridView_fuliao.Rows[j].Cells[6].Value = yuancailiaobean.Selectresult[j, 4].ToString().Trim();
                    if (isSelectByPeriod)
                    {
                        this.dataGridView_fuliao.Rows[j].Cells[7].Value = yuancailiaobean.Selectresult[j, 5].ToString().Trim();
                        this.dataGridView_fuliao.Rows[j].Cells[5].Value = yuancailiaobean.Selectresult[j, 6].ToString().Trim();
                        this.dataGridView_fuliao.Rows[j].DefaultCellStyle.BackColor = Color.LightGray;
                    }
                    else
                    {
                        Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
                        //取得实际重量
                        string shijizhongliang = service.chukushijiZhongliang(fuliao_startday.Value.ToString("yyyyMMdd"), fuliao_endDay.Value.ToString("yyyyMMdd"), yuancailiaobean.Selectresult[j, 0].ToString().Trim(), yuancailiaobean.Selectresult[j, 1].ToString().Trim(), yuancailiaobean.Selectresult[j, 2].ToString().Trim(), "2");

                        if (Command.nullToSpace(shijizhongliang).Equals(""))
                        {
                            this.dataGridView_fuliao.Rows[j].Cells[5].Value = yuancailiaobean.Selectresult[j, 5].ToString().Trim();
                        }
                        else
                        {
                            this.dataGridView_fuliao.Rows[j].Cells[5].Value = shijizhongliang;
                        }
                    }
                    this.fuliao_details.Text = "查看详细";

                }
                float zhongliang = 0;
                if (!isSelectByPeriod)
                {
                    for (int j = 0; j < yuancailiaobean.Resultrow; j++)
                    {
                        //品名，规格不一致时添加新行
                        if (!"".Equals(beforePinming) && (!beforePinming.Equals(yuancailiaobean.Selectresult[j, 0].ToString().Trim()) || !beforeGuige.Equals(yuancailiaobean.Selectresult[j, 1].ToString().Trim())))
                        {
                            DataGridViewRow row = new DataGridViewRow();
                            row.DefaultCellStyle.BackColor = Color.White;
                            this.dataGridView_fuliao.Rows.InsertRange(count, row);
                            this.dataGridView_fuliao.Rows[count].Cells[5].Style.BackColor = Color.LightCyan;
                            this.dataGridView_fuliao.Rows[count].Cells[5].Value = Convert.ToString(zhongliang);
                            this.dataGridView_fuliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_fuliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_fuliao.Rows[count - 1].Cells[1].Value).ToString();
                            this.dataGridView_fuliao.Rows[count].Cells[5].Value = Convert.ToString(shijiTotal);
                            count++;
                            shijiTotal = 0;
                            zhongliang = 0;
                        }

                        beforePinming = yuancailiaobean.Selectresult[j, 0].ToString().Trim();
                        beforeGuige = yuancailiaobean.Selectresult[j, 1].ToString().Trim();
                        shijiTotal += float.Parse(yuancailiaobean.Selectresult[j, 5].ToString().Trim());
                        zhongliang += float.Parse(this.dataGridView_fuliao.Rows[count].Cells[5].Value.ToString());
                        count++;

                    }

                    //最后一条

                    DataGridViewRow rowLast = new DataGridViewRow();
                    rowLast.DefaultCellStyle.BackColor = Color.White;
                    this.dataGridView_fuliao.Rows.Add(rowLast);
                    this.dataGridView_fuliao.Rows[count].Cells[5].Style.BackColor = Color.LightCyan;
                    this.dataGridView_fuliao.Rows[count].Cells[5].Value = Convert.ToString(zhongliang);
                    this.dataGridView_fuliao.Rows[count].Cells[10].Value = Command.nullToObject(this.dataGridView_fuliao.Rows[count - 1].Cells[0].Value).ToString() + Command.nullToObject(this.dataGridView_fuliao.Rows[count - 1].Cells[1].Value).ToString();
                    //this.dataGridView_fuliao.Rows[count].Cells[6].Value = Convert.ToString(shijiTotal);
                }
                }  
            
            
        }

        private void btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("dfdf");
        }

        private void dataGridView_yuanliao_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try{
                if (this.dataGridView_yuanliao.SelectedCells[0].ColumnIndex == 9)
                {

                    DataGridViewCell shengyu_zhongliang = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[7];

                    Form_chukumingxi_details frmMixingDetails = new Form_chukumingxi_details();
                    Object zhongliang_total = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[6].Value;
                    Object zhongliang_shengyu = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[7].Value;
                    Object pinming = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[0].Value;
                    Object guige = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[1].Value;
                    Object beizhu = this.dataGridView_yuanliao.Rows[this.dataGridView_yuanliao.SelectedCells[0].RowIndex].Cells[2].Value;
                    //string dateStartDate = this.dateTimePicker_startday.Value.ToString("yyyyMMdd");
                    //string dateEndDate = this.dateTimePicker_endday.Value.ToString("yyyyMMdd");
                    DateTimePicker dateStartDate = this.dateTimePicker_startday;
                    DateTimePicker dateEndDate = this.dateTimePicker_endday;


                    Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.selectByPeriod(0, pinming, guige, beizhu, dateStartDate, dateEndDate);



                    frmMixingDetails.selfShow(0, bean, pinming, zhongliang_total, zhongliang_shengyu, dateStartDate, dateEndDate);

                }
            }
            catch (Exception)
            {

            }

            
        }

        /**
         * 
         * 原料tab初始化
         * 
         * 
         * */
        private void initYuanLiao() {

            string lastJijiDate = getLastJijiDate("0");
            if ("".Equals(lastJijiDate))
            {
                //开始日期
                string startYear = this.dateTimePicker_startday.Value.Year.ToString();
                string startMonth = this.dateTimePicker_startday.Value.Month.ToString();
                this.dateTimePicker_startday.Text = startYear + "年" + startMonth + "月" + "1日";
                yuanliaoLastJijiDate = new DateTime(9999, 12, 31);
            }
            else
            {
                //开始日期
                string startYear = lastJijiDate.Substring(0,4);
                string startMonth = lastJijiDate.Substring(4, 2);
                string startDay = lastJijiDate.Substring(6, 2);
                DateTime dt = new DateTime(int.Parse(startYear), int.Parse(startMonth), int.Parse(startDay));
                dt = dt.AddDays(1);
                yuanliaoLastJijiDate = dt;

                this.dateTimePicker_startday.Value = dt;

            }

            
            //结束日期
            this.dateTimePicker_endday.Value = this.dateTimePicker_startday.Value.AddMonths(1).AddDays(-1);

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(0);

            if (bean != null)
            {
                datagridviewadddata(bean,0,false);
                this.btn_save.Enabled = true;
                button9.Enabled = true;
                isLoad = false;
            }
            else
            {
                
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.btn_save.Enabled = false;
                button9.Enabled = false;
                isLoad = false;
                return;
            }
            
        }

        /**
 * 
 * 调味料tab初始化
 * 
 * 
 * */
        private void initTiaoliao(int tabIndex)
        {
            string lastJijiDate = getLastJijiDate("1");

            if ("".Equals(lastJijiDate))
            {
                //开始日期
                string startYear = this.tiaoliao_startday.Value.Year.ToString();
                string startMonth = this.tiaoliao_startday.Value.Month.ToString();
                tiaoliaoLastJijiDate = new DateTime(9999,12,31);
                this.tiaoliao_startday.Text = startYear + "年" + startMonth + "月" + "1日";

            }
            else
            {
                //开始日期
                string startYear = lastJijiDate.Substring(0, 4);
                string startMonth = lastJijiDate.Substring(4, 2);
                string startDay = lastJijiDate.Substring(6, 2);
                DateTime dt = new DateTime(int.Parse(startYear), int.Parse(startMonth), int.Parse(startDay));
                dt = dt.AddDays(1);
                tiaoliaoLastJijiDate = dt;

                this.tiaoliao_startday.Value= dt;

            }

            //结束日期
            this.tiaoliao_endday.Value = this.tiaoliao_startday.Value.AddMonths(1).AddDays(-1);

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(1);

            if (bean != null)
            {
                datagridviewadddata(bean, 1,false);
                this.tiaoliao_exportExcel.Enabled = true;
                button10.Enabled = true;
                isLoad = false;
            }
            else
            {

                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.tiaoliao_exportExcel.Enabled = false;
                button10.Enabled = false;
                isLoad = false;
                return;
            }
            
        }

        /**
* 
*辅料tab初始化
* 
* 
* */
        private void initFuliao(int tabIndex)
        {

            string lastJijiDate = getLastJijiDate("2");

            if ("".Equals(lastJijiDate))
            {
                //开始日期
                string startYear = this.fuliao_startday.Value.Year.ToString();
                string startMonth = this.fuliao_startday.Value.Month.ToString();
                this.fuliao_startday.Text = startYear + "年" + startMonth + "月" + "1日";
                fuliaoLastJijiDate = new DateTime(9999, 12, 31);
            }
            else
            {
                //开始日期
                string startYear = lastJijiDate.Substring(0, 4);
                string startMonth = lastJijiDate.Substring(4, 2);
                string startDay = lastJijiDate.Substring(6, 2);
                DateTime dt = new DateTime(int.Parse(startYear), int.Parse(startMonth), int.Parse(startDay));
                dt = dt.AddDays(1);
                fuliaoLastJijiDate = dt;
                this.fuliao_startday.Value = dt;

            }

            //结束日期
            this.fuliao_endDay.Value = this.fuliao_startday.Value.AddMonths(1).AddDays(-1);

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(2);

            if (bean != null)
            {
                datagridviewadddata(bean, 2,false);
                this.fuliao_exportExcel.Enabled = true;
                button11.Enabled = true;
                isLoad = false;
            }
            else
            {

                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.fuliao_exportExcel.Enabled = false;
                button11.Enabled = false;
                isLoad = false;
                return;
            }

        }

   private Object selectByPeriod(int tabIndex , Object pinming,Object guige,Object beizhu, DateTimePicker dateStartDate, DateTimePicker dateEndDate)
        {
            if (tabIndex == 0)
            {
                Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();
                Bean.Chukumingxi_yuancailiaoBean mingxiBean = new Bean.Chukumingxi_yuancailiaoBean();
                mingxiBean.Jiji_startday = dateStartDate.Value.ToString("yyyyMMdd");
                mingxiBean.Jiji_endday = dateEndDate.Value.ToString("yyyyMMdd");
                mingxiBean.Cuku_type = "0";
                mingxiBean.Yuanliaocuku_pinming = pinming.ToString();
                mingxiBean.Chuku_guige = guige.ToString();
                mingxiBean.Chuku_beizhu = beizhu.ToString();
                
                bean = ChukumingxiService.chukuguanli_ServiceDetailsSelect(mingxiBean);
                return bean;
            }
            else if (tabIndex == 1)
            {
                Bean.Chukuguanli_tiaoweiliaoBean bean = new FWMS.Bean.Chukuguanli_tiaoweiliaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();
                Bean.Chukumingxi_yuancailiaoBean mingxiBean = new Bean.Chukumingxi_yuancailiaoBean();
                mingxiBean.Jiji_startday = dateStartDate.Value.ToString("yyyyMMdd");
                mingxiBean.Jiji_endday = dateEndDate.Value.ToString("yyyyMMdd");
                mingxiBean.Cuku_type = "1";
                mingxiBean.Yuanliaocuku_pinming = pinming.ToString();
                mingxiBean.Chuku_guige = guige.ToString();
                mingxiBean.Chuku_beizhu = beizhu.ToString();
                bean = ChukumingxiService.chukuguanli_ServiceDetailsTiaoLiaoSelect(mingxiBean);
                return bean;
            }
            else if (tabIndex == 2)
            {
                Bean.Chukuguanli_fuliaoBean bean = new FWMS.Bean.Chukuguanli_fuliaoBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();
                Bean.Chukumingxi_yuancailiaoBean mingxiBean = new Bean.Chukumingxi_yuancailiaoBean();
                mingxiBean.Jiji_startday = dateStartDate.Value.ToString("yyyyMMdd");
                mingxiBean.Jiji_endday = dateEndDate.Value.ToString("yyyyMMdd");
                mingxiBean.Cuku_type = "2";
                mingxiBean.Yuanliaocuku_pinming = pinming.ToString();
                mingxiBean.Chuku_guige = guige.ToString();
                mingxiBean.Chuku_beizhu = beizhu.ToString();
                bean = ChukumingxiService.chukuguanli_ServiceDetailsFiLiaoSelect(mingxiBean);
                return bean;

            }
            else if (tabIndex == 3)
            {
                Bean.Chukuguanli_chanpinBean bean = new FWMS.Bean.Chukuguanli_chanpinBean();
                Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();
                Bean.Chukumingxi_yuancailiaoBean mingxiBean = new Bean.Chukumingxi_yuancailiaoBean();
                mingxiBean.Jiji_startday = dateStartDate.Value.ToString("yyyyMMdd");
                mingxiBean.Jiji_endday = dateEndDate.Value.ToString("yyyyMMdd");
                mingxiBean.Cuku_type = "3";
                mingxiBean.Yuanliaocuku_pinming = pinming.ToString();
                mingxiBean.Chuku_guige = guige.ToString();
                mingxiBean.Chuku_beizhu = beizhu.ToString();
                bean = ChukumingxiService.chukuguanli_ServiceDetailsChengpinSelect(mingxiBean);
                return bean;
            }
            return null;

        }

        private void dataGridView_yuanliao_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            /**
            if (this.dataGridView_yuanliao.CurrentRow.Cells[0].Value != null)
            {
                MessageBox.Show("请在实际使用重量总重处填写！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.dataGridView_yuanliao.SelectedCells[0].Value = this.dataGridView_yuanliao.CurrentRow.Cells[6].Value;
                return;
            }
             **/

            DataGridViewCell shengyu_zhongliang = this.dataGridView_yuanliao.CurrentCell;
            if (shengyu_zhongliang.Value != null && ! shengyu_zhongliang.Value.ToString().Trim().Equals(""))
            {
                if (!COMMAND.Command.isFormatByReg(shengyu_zhongliang.Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                {
                    this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                    MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else if (shengyu_zhongliang.Value.ToString().Trim().Equals(""))
                {

                    return;
                }
                else if (this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value == null || "".Equals(this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value.ToString().Trim()))
                {

                    //输入校验
                    string zhongliang_shengyu = this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    float sumToatle = 0;

                    foreach (DataGridViewRow row in this.dataGridView_yuanliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                sumToatle += float.Parse(row.Cells[6].Value.ToString());
                            }
                        }
                        
                    }
                    int i = 0;
                    double countCalculteAfter = 0;
                    foreach (DataGridViewRow row in this.dataGridView_yuanliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string shijichukuzhongliang= row.Cells[6].Value.ToString();
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_yuanliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                if ((this.dataGridView_yuanliao.SelectedCells[0].RowIndex - 1)==i)
                                {
                                    row.Cells[7].Value = Command.toString((Math.Round(float.Parse(zhongliang_shengyu) - Math.Round(countCalculteAfter,3),3)));
                                }
                                else
                                {
                                    row.Cells[7].Value = Command.toString(Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3));
                                    countCalculteAfter += Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3);
                                }
                                

                                //row.Cells[7].Value = Math.Round(float.Parse(shijichukuzhongliang) - float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu),3) ;
                            }
                            
                        }
                        i++;

                    }


                }
            }

            
        }

        /**
         * 
         * 画面输入项目检查
         * 
         * 
         * */
        public string inputCheck()
        {
            DateTime dateStartDate = this.dateTimePicker_startday.Value;
             DateTime dateEndDate = this.dateTimePicker_endday.Value;
            if (dateStartDate == null )
            {
                return "集计开始日期必须输入！";
            }else if(dateEndDate == null)
            {
                return "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Days;
            if (period < 0)
            {
                return "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 32)
            {
                return "集计时间范围超出，请重新选择集计时间！";
            }


            string errorMsg = "";
            foreach (DataGridViewRow row in this.dataGridView_yuanliao.Rows)
            {
                if (row.Cells[7].Value!=null)
                {
                    string zhongliang_shengyu = row.Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        errorMsg = "实际使用重量只能输入小数位数最大为三位的数值！";
                    }
                }
            }


            return errorMsg;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {

            if (!"99991231".Equals(yuanliaoLastJijiDate.ToString("yyyyMMdd"))&&yuanliaoLastJijiDate != dateTimePicker_startday.Value)
            {
                DateTime dt = yuanliaoLastJijiDate.AddDays(-1);
                if (MessageBox.Show("上次集计时间截止为" + dt.Year + "年" + dt.Month + "月" + dt.Day + "日,如果继续集计可能造成数据不完整或数据丢失！", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
            }
            //剩余重量检验
            int i = 0;
            int errorNum = 0;
            string errorMessage = "";
            foreach(DataGridViewRow row in this.dataGridView_yuanliao.Rows)
            {
                
                if(row.Cells[7].Value != null)
                {
                    if (row.Cells[0].Value == null)
                    {
                        if (!COMMAND.Command.isFormatByReg(row.Cells[7].Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                        {
                            this.dataGridView_yuanliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                            // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            errorMessage = "实际使用重量总量输入有误！";
                            errorNum++;
                            
                        }
                        i++;
                        continue;
                    }
                    string zhongliang_shiji = row.Cells[7].Value.ToString();
                    if (!COMMAND.Command.isFormatByReg(zhongliang_shiji, "^(-?[0-9])+(.[0-9]{1,3})?$"))
                    {
                        this.dataGridView_yuanliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                       // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量输入有误！";
                        errorNum++;
                    }
                        /*
                    else if (float.Parse(row.Cells[7].Value.ToString()) > float.Parse(row.Cells[6].Value.ToString()))
                    {
                        this.dataGridView_yuanliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        //MessageBox.Show("剩余重量不能大于重量(集计钱)！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量不能大于实际出库重量！";

                        errorNum++;
                    }*/
                }

                i++;
            }

            if (errorNum > 0)
            {
                MessageBox.Show(errorMessage, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //其他输入检验
            string errorCheckMesg = inputCheck();

            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int numcont = this.dataGridView_yuanliao.Rows.Count;
            Selectresult = new object[numcont, 9];

            int num = 0;
            int resultRow = 0;
            foreach (DataGridViewRow row in this.dataGridView_yuanliao.Rows)
            {
                if (row.Cells[0].Value != null && !"".Equals(row.Cells[0].Value.ToString()))
                {

                    //品名
                    Selectresult[resultRow, 0] = row.Cells[0].Value;
                    //规格
                    Selectresult[resultRow, 1] = row.Cells[1].Value;
                    //备注
                    Selectresult[resultRow, 2] = row.Cells[2].Value;
                    //单位
                    Selectresult[resultRow, 3] = row.Cells[3].Value;
                    //实际出库重量
                    Selectresult[resultRow, 4] = row.Cells[6].Value;
                    //剩余
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        Selectresult[resultRow, 5] = row.Cells[6].Value;
                    }
                    else
                    {
                        Selectresult[resultRow, 5] = row.Cells[7].Value;
                    }


                    Selectresult[resultRow, 6] = dateTimePicker_startday.Value.ToString("yyyyMMdd");
                    Selectresult[resultRow, 7] = dateTimePicker_endday.Value.ToString("yyyyMMdd");
                    if (row.Cells[5].Value == null)
                    {
                        Selectresult[resultRow, 8] = "0";
                    }
                    else
                    {
                        Selectresult[resultRow, 8] = row.Cells[5].Value;
                    }

                    resultRow++;

                }
                else
                {
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        row.DefaultCellStyle.BackColor = Color.LightPink;
                        MessageBox.Show("实际出库总重量必须输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                num++;
            }

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();
            bean.Selectresult = Selectresult;
            bean.Resultrow = resultRow;
            bool chukumingsiUpdate = service.chukumingxi_yuancailiaoServicUpdate(bean);
            //bool chukuguanliUpdate = service.chukuguanli_yuancailiaoServicUpdate(bean, this.dateTimePicker_startday.Value.ToString("yyyyMMdd"), this.dateTimePicker_endday.Value.ToString("yyyyMMdd"));
            bool kuchunguanliUPdate = service.kuchunguanli_update(this.dataGridView_yuanliao,"0");

            if (!chukumingsiUpdate)
            {
                MessageBox.Show("数据保存失败，请重新操作！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("数据保存成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
     
            }

        }

        //导出EXCEl表
        private void exportExcel(string template,string sheet, DataGridView gridview)
        { 
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();

            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + "\\"+template);

            int count = 0;
            for (int i = 0; i < gridview.Rows.Count; i++)
            {
                if (gridview.Rows[i].Cells[0].Value != null)
                {
                    //品名
                    importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 1, gridview.Rows[i].Cells[0].Value.ToString().Trim());

                    //规格
                    if (gridview.Rows[i].Cells[1].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 2, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 2, gridview.Rows[i].Cells[1].Value.ToString().Trim());
                    }

                    //单位
                    if (gridview.Rows[i].Cells[3].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 3, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 3, gridview.Rows[i].Cells[3].Value.ToString().Trim());
                    }

                    //单价
                    if (gridview.Rows[i].Cells[4].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 4, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 4, gridview.Rows[i].Cells[4].Value.ToString().Trim());
                    }

                    //件数
                    if (gridview.Rows[i].Cells[5].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 5, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 5, gridview.Rows[i].Cells[5].Value.ToString().Trim());
                    }

                    //实际出库重量
                    if (gridview.Rows[i].Cells[7].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 6, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 6, gridview.Rows[i].Cells[7].Value.ToString().Trim());
                    }
                    //总值
                    if (gridview.Rows[i].Cells[8].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 7, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 7, gridview.Rows[i].Cells[8].Value.ToString().Trim());
                    }

                    //备注
                    if (gridview.Rows[i].Cells[2].Value == null)
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 8, "");
                    }
                    else
                    {
                        importexcel.SetCellValue(importexcel.GetSheet(sheet4name), 5 + count, 8, gridview.Rows[i].Cells[2].Value.ToString().Trim());
                    }
                    count++;
                }
                else
                {
                    continue;
                }
            }

            if (count < 21)
            {
                importexcel.setRowsHidden(importexcel.GetSheet(sheet+"1"), 27, 53);
            }
            else if (count > 40 && count < 61)
            {
                importexcel.setRowsHidden(importexcel.GetSheet(sheet + "2"), 27, 53);
            }
            else if (count > 80 && count < 101)
            {
                importexcel.setRowsHidden(importexcel.GetSheet(sheet + "3"), 27, 53);
            }

            importexcel.SaveAs(path2);
            importexcel.Close();
            MessageBox.Show("数据导出EXCEL成功,请注意保存数据！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }
        private void dataGridView_yuanliao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (this.dataGridView_yuanliao.CurrentCell.ColumnIndex == 7)
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 46) && (e.KeyChar != 8) && (e.KeyChar != 13) && e.KeyChar != 45)
                    e.Handled = true;
                else
                    e.Handled = false;
            }       
        }

        private void dataGridView_yuanliao_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView_yuanliao.CurrentCell.ColumnIndex == 7)
            {
                Control cotrol = (Control)e.Control;
                cotrol.KeyPress += new KeyPressEventHandler(dataGridView_yuanliao_KeyPress);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.tabControl1.SelectedIndex)
            { 
                case 0:
                    COMMAND.CommandBean.cailiaostyle = 0;
                    break;

                case 1:
                    if (isTiaoWeiLiaoLoad == true)
                    {
                        initTiaoliao(1);
                        isTiaoWeiLiaoLoad = false;
                    }
                    COMMAND.CommandBean.cailiaostyle = 1;
                    break;
                case 2:
                    if (isFuLiaoLoad == true)
                    {
                        initFuliao(2);
                        isFuLiaoLoad = false;
                    }
                    COMMAND.CommandBean.cailiaostyle = 2;
                    break;

                
            }
        }


        private void tiaoliao_endday_ValueChanged(object sender, EventArgs e)
        {
            if (isTiaoWeiLiaoLoad == false)
            {
                Bean.Chukuguanli_tiaoweiliaoBean bean = (Bean.Chukuguanli_tiaoweiliaoBean)this.getData(1);
                if (bean != null)
                {
                    datagridviewadddata(bean, 1,false);
                }
                else
                {
                    if (!isLoad)
                    {
                        MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                
            }
            
        }

        private void tiaoliao_startday_ValueChanged(object sender, EventArgs e)
        {
            if (isTiaoWeiLiaoLoad == false)
            {
                Bean.Chukuguanli_tiaoweiliaoBean bean = (Bean.Chukuguanli_tiaoweiliaoBean)this.getData(1);
                if (bean != null)
                {
                    datagridviewadddata(bean, 1,false);
                }
                else
                {
                    if (!isLoad)
                    {
                        MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

            }
         }

        private void dataGridView_yuanliao_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = this.dataGridView_yuanliao;
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            DataGridViewCell cellBtn = row.Cells[row.Cells.Count - 2];
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            Padding defaultPadding = new Padding(0, 0, 0, 0);
            Padding userPadding = new Padding(0, cellBtn.Size.Height, 0, -cellBtn.Size.Height);
            if (row.Cells[0].Value == null)
            {
                style.Padding = userPadding;
            }
            else
            {
                style.Padding = defaultPadding;
            }
            cellBtn.Style = style;
        }

        private void yuanliao_search_Click(object sender, EventArgs e)
        {

            //其他输入检验
            string errorCheckMesg = "";

            DateTime dateStartDate = this.dateTimePicker_startday.Value;
            DateTime dateEndDate = this.dateTimePicker_endday.Value;
            if (dateStartDate == null)
            {
                errorCheckMesg =  "集计开始日期必须输入！";
            }
            else if (dateEndDate == null)
            {
                errorCheckMesg = "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Hours;
            if (period < 0)
            {
                errorCheckMesg = "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 30)
            {
                errorCheckMesg = "集计时间范围超出，请重新选择集计时间！";
            }



            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(0);
            if (bean != null)
            {
                datagridviewadddata(bean, 0,false);
                this.btn_save.Enabled = true;
                button9.Enabled = true;
            }
            else
            {
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.btn_save.Enabled = false;
                button9.Enabled = true;
            }
        }


        private void dataGridView_tiaoliao_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
             DataGridView dgv = this.dataGridView_tiaoliao;
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            DataGridViewCell cellBtn = row.Cells[row.Cells.Count - 2];
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            Padding defaultPadding = new Padding(0, 0, 0, 0);
            Padding userPadding = new Padding(0, cellBtn.Size.Height, 0, -cellBtn.Size.Height);
            if (row.Cells[0].Value == null)
            {
                style.Padding = userPadding;
            }
            else
            {
                style.Padding = defaultPadding;
            }
            cellBtn.Style = style;
        }
        /**
 * 
 * 调料输入项目检查
 * 
 * 
 * */
        public string inputCheckTiaoLiao()
        {
            DateTime dateStartDate = this.tiaoliao_startday.Value;
            DateTime dateEndDate = this.tiaoliao_endday.Value;
            if (dateStartDate == null)
            {
                return "集计开始日期必须输入！";
            }
            else if (dateEndDate == null)
            {
                return "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Days;
            if (period < 0)
            {
                return "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 32)
            {
                return "集计时间范围超出，请重新选择集计时间！";
            }


            string errorMsg = "";
            foreach (DataGridViewRow row in this.dataGridView_tiaoliao.Rows)
            {
                if (row.Cells[7].Value != null)
                {
                    string zhongliang_shengyu = row.Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        errorMsg = "实际使用重量只能输入小数位数最大为三位的数值！";
                    }
                }
            }


            return errorMsg;
        }
        
                    /**
 * 
 * 辅料输入项目检查
 * 
 * 
 * */
        public string inputCheckFuLiao()
        {
            DateTime dateStartDate = this.fuliao_startday.Value;
            DateTime dateEndDate = this.fuliao_endDay.Value;
            if (dateStartDate == null)
            {
                return "集计开始日期必须输入！";
            }
            else if (dateEndDate == null)
            {
                return "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Days;
            if (period < 0)
            {
                return "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 32)
            {
                return "集计时间范围超出，请重新选择集计时间！";
            }


            string errorMsg = "";
            foreach (DataGridViewRow row in this.dataGridView_fuliao.Rows)
            {
                if (row.Cells[7].Value != null)
                {
                    string zhongliang_shengyu = row.Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        errorMsg = "实际使用重量只能输入小数位数最大为三位的数值！";
                    }
                }
            }


            return errorMsg;
        }



        private void tiaoliao_search_Click(object sender, EventArgs e)
        {

            //其他输入检验
            string errorCheckMesg = "";

            DateTime dateStartDate = this.tiaoliao_startday.Value;
            DateTime dateEndDate = this.tiaoliao_endday.Value;
            if (dateStartDate == null)
            {
                errorCheckMesg = "集计开始日期必须输入！";
            }
            else if (dateEndDate == null)
            {
                errorCheckMesg = "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Hours;
            if (period < 0)
            {
                errorCheckMesg = "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 30)
            {
                errorCheckMesg = "集计时间范围超出，请重新选择集计时间！";
            }
            

            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(1);
            if (bean != null)
            {
                datagridviewadddata(bean, 1,false);
                this.tiaoliao_exportExcel.Enabled = true;
                button10.Enabled = true;
            }
            else
            {
                this.tiaoliao_exportExcel.Enabled = false;
                button10.Enabled = true;
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dataGridView_tiaoliao_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView_tiaoliao_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            /**if (this.dataGridView_tiaoliao.CurrentRow.Cells[0].Value != null)
            {
                MessageBox.Show("请在实际使用重量总重处填写！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.dataGridView_tiaoliao.SelectedCells[0].Value = this.dataGridView_tiaoliao.CurrentRow.Cells[6].Value;
                return;
            }
            **\
             */
            DataGridViewCell shengyu_zhongliang = this.dataGridView_tiaoliao.CurrentCell;
            if (shengyu_zhongliang.Value != null && !shengyu_zhongliang.Value.ToString().Trim().Equals(""))
            {
                if (!COMMAND.Command.isFormatByReg(shengyu_zhongliang.Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                {
                    this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                    MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else if (shengyu_zhongliang.Value.ToString().Trim().Equals(""))
                {

                    return;
                }
                else if (this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value == null || "".Equals(this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value.ToString().Trim()))
                {

                    //输入校验
                    string zhongliang_shengyu = this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    float sumToatle = 0;

                    foreach (DataGridViewRow row in this.dataGridView_tiaoliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                sumToatle += float.Parse(row.Cells[6].Value.ToString());
                            }
                        }

                    }
                    int i = 0;
                    double countCalculteAfter = 0;
                    foreach (DataGridViewRow row in this.dataGridView_tiaoliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string shijichukuzhongliang = row.Cells[6].Value.ToString();
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_tiaoliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                if ((this.dataGridView_tiaoliao.SelectedCells[0].RowIndex - 1) == i)
                                {
                                    row.Cells[7].Value = Convert.ToString(Math.Round(float.Parse(zhongliang_shengyu) - Math.Round(countCalculteAfter, 3), 3));
                                }
                                else
                                {

                                    row.Cells[7].Value = Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3);
                                    countCalculteAfter += Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3);
                                }


                                //row.Cells[7].Value = Math.Round(float.Parse(shijichukuzhongliang) - float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu),3) ;
                            }

                        }
                        i++;

                    }


                }
            }
        }

        private void dataGridView_tiaoliao_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (this.dataGridView_tiaoliao.SelectedCells[0].ColumnIndex == 9)
                {

                    DataGridViewCell shengyu_zhongliang = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[7];

                    Form_chukumingxi_details frmMixingDetails = new Form_chukumingxi_details();
                    Object zhongliang_total = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[6].Value;
                    Object zhongliang_shengyu = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[7].Value;
                    Object pinming = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[0].Value;
                    Object guige = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[1].Value;
                    Object beizhu = this.dataGridView_tiaoliao.Rows[this.dataGridView_tiaoliao.SelectedCells[0].RowIndex].Cells[2].Value;
                    //string dateStartDate = this.dateTimePicker_startday.Value.ToString("yyyyMMdd");
                    //string dateEndDate = this.dateTimePicker_endday.Value.ToString("yyyyMMdd");
                    DateTimePicker dateStartDate = this.tiaoliao_startday;
                    DateTimePicker dateEndDate = this.tiaoliao_endday;


                    Bean.Chukuguanli_tiaoweiliaoBean bean = (Bean.Chukuguanli_tiaoweiliaoBean)this.selectByPeriod(1, pinming, guige, beizhu, dateStartDate, dateEndDate);



                    frmMixingDetails.selfShow(1, bean, pinming, zhongliang_total, zhongliang_shengyu, dateStartDate, dateEndDate);

                }
            }
            catch (Exception)
            {

            }
        }

        private void tiaoliao_exportExcel_Click(object sender, EventArgs e)
        {
            if (!"99991231".Equals(tiaoliaoLastJijiDate.ToString("yyyyMMdd")) && tiaoliaoLastJijiDate != tiaoliao_startday.Value)
            {
                DateTime dt = tiaoliaoLastJijiDate.AddDays(-1);
                if (MessageBox.Show("上次集计时间截止为" + dt.Year + "年" + dt.Month + "月" + dt.Day + "日,如果继续集计可能造成数据不完整或数据丢失！", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
            }
            
            //剩余重量检验
            int i = 0;
            int errorNum = 0;
            string errorMessage = "";
            foreach (DataGridViewRow row in this.dataGridView_tiaoliao.Rows)
            {

                if (row.Cells[7].Value != null)
                {
                    if (row.Cells[0].Value == null)
                    {
                        if (!COMMAND.Command.isFormatByReg(row.Cells[7].Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                        {
                            this.dataGridView_tiaoliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                            // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            errorMessage = "实际使用重量总量输入有误！";
                            errorNum++;
                        }
                        i++;
                        continue;
                    }
                    string zhongliang_shiji = row.Cells[7].Value.ToString();
                    if (!COMMAND.Command.isFormatByReg(zhongliang_shiji, "^(-?[0-9])+(.[0-9]{1,3})?$"))
                    {
                        this.dataGridView_tiaoliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量输入有误！";
                        errorNum++;
                    }
                        /*
                    else if (float.Parse(row.Cells[7].Value.ToString()) > float.Parse(row.Cells[6].Value.ToString()))
                    {
                        this.dataGridView_tiaoliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        //MessageBox.Show("剩余重量不能大于重量(集计钱)！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量不能大于实际出库重量！";

                        errorNum++;
                    }*/
                }

                i++;
            }

            if (errorNum > 0)
            {
                MessageBox.Show(errorMessage, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //其他输入检验
            string errorCheckMesg = inputCheckTiaoLiao();

            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int numcont = this.dataGridView_tiaoliao.Rows.Count;
            Selectresult = new object[numcont, 9];

            int num = 0;
            int resultRow = 0;
            foreach (DataGridViewRow row in this.dataGridView_tiaoliao.Rows)
            {
                if (row.Cells[0].Value != null && !"".Equals(row.Cells[0].Value.ToString()))
                {

                    //品名
                    Selectresult[resultRow, 0] = row.Cells[0].Value;
                    //规格
                    Selectresult[resultRow, 1] = row.Cells[1].Value;
                    //备注
                    Selectresult[resultRow, 2] = row.Cells[2].Value;
                    //单位
                    Selectresult[resultRow, 3] = row.Cells[3].Value;
                    //实际出库重量
                    Selectresult[resultRow, 4] = row.Cells[6].Value;
                    //剩余
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        Selectresult[resultRow, 5] = row.Cells[6].Value;
                    }
                    else
                    {
                        Selectresult[resultRow, 5] = row.Cells[7].Value;
                    }
                    

                    Selectresult[resultRow, 6] = this.tiaoliao_startday.Value.ToString("yyyyMMdd");
                    Selectresult[resultRow, 7] = this.tiaoliao_endday.Value.ToString("yyyyMMdd");
                    if (row.Cells[5].Value == null)
                    {
                        Selectresult[resultRow, 8] = "0";
                    }
                    else
                    {
                        Selectresult[resultRow, 8] = row.Cells[5].Value;
                    }
                   

                    resultRow++;

                }
                else
                {
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        row.DefaultCellStyle.BackColor = Color.LightPink;
                        MessageBox.Show("实际出库总重量必须输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                num++;
            }

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_tiaoweiliaoBean bean = new FWMS.Bean.Chukuguanli_tiaoweiliaoBean();
            bean.Selectresult = Selectresult;
            bean.Resultrow = resultRow;
            bool chukumingsiUpdate = service.chukumingxi_tiaoWeilLiaoServicUpdate(bean);
            // bool chukuguanliUpdate = service.chukuguanli_yuancailiaoServicUpdate(bean, this.dateTimePicker_startday.Value.ToString("yyyyMMdd"), this.dateTimePicker_endday.Value.ToString("yyyyMMdd"));
            bool kuchunguanliUPdate = service.kuchunguanli_update(this.dataGridView_tiaoliao,"1");


            if (!chukumingsiUpdate)
            {
                MessageBox.Show("保存失败，请重新保存！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("数据保存成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void dataGridView_fuliao_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = this.dataGridView_fuliao;
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            DataGridViewCell cellBtn = row.Cells[row.Cells.Count - 2];
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            Padding defaultPadding = new Padding(0, 0, 0, 0);
            Padding userPadding = new Padding(0, cellBtn.Size.Height, 0, -cellBtn.Size.Height);
            if (row.Cells[0].Value == null)
            {
                style.Padding = userPadding;
            }
            else
            {
                style.Padding = defaultPadding;
            }
            cellBtn.Style = style;
        }

        private void dataGridView_fuliao_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            /**if (this.dataGridView_fuliao.CurrentRow.Cells[0].Value != null)
            {
                MessageBox.Show("请在实际使用重量总重处填写！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.dataGridView_fuliao.SelectedCells[0].Value = this.dataGridView_fuliao.CurrentRow.Cells[6].Value;
                return;
            }*/

            DataGridViewCell shengyu_zhongliang = this.dataGridView_fuliao.CurrentCell;
            if (shengyu_zhongliang.Value != null && !shengyu_zhongliang.Value.ToString().Trim().Equals(""))
            {
                if (!COMMAND.Command.isFormatByReg(shengyu_zhongliang.Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                {
                    this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                    MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else if (shengyu_zhongliang.Value.ToString().Trim().Equals(""))
                {

                    return;
                }
                else if (this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value == null || "".Equals(this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].Cells[0].Value.ToString().Trim()))
                {

                    //输入校验
                    string zhongliang_shengyu = this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].Cells[7].Value.ToString();
                    Regex reg = new Regex("^(-?[0-9])+(.[0-9]{1,3})?$");
                    Match m = reg.Match(zhongliang_shengyu);
                    if (!m.Success)
                    {
                        this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        MessageBox.Show("实际使用重量只能输入小数位数最大为三位的数值！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    float sumToatle = 0;

                    foreach (DataGridViewRow row in this.dataGridView_fuliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                sumToatle += float.Parse(row.Cells[6].Value.ToString());
                            }
                        }

                    }
                    int i = 0;
                    double countCalculteAfter = 0;
                    foreach (DataGridViewRow row in this.dataGridView_fuliao.Rows)
                    {
                        if (row.Cells[0].Value != null)
                        {
                            string shijichukuzhongliang = row.Cells[6].Value.ToString();
                            string pingmingGuige = Command.nullToObject(row.Cells[0].Value).ToString() + Command.nullToObject(row.Cells[1].Value).ToString();
                            if (pingmingGuige.Equals(this.dataGridView_fuliao.Rows[shengyu_zhongliang.RowIndex].Cells[10].Value.ToString()))
                            {
                                if ((this.dataGridView_fuliao.SelectedCells[0].RowIndex - 1) == i)
                                {
                                    row.Cells[7].Value = Convert.ToString(Math.Round(float.Parse(zhongliang_shengyu) - Math.Round(countCalculteAfter, 3), 3));
                                }
                                else
                                {

                                    row.Cells[7].Value = Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3);
                                    countCalculteAfter += Math.Round(float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu), 3);
                                }


                                //row.Cells[7].Value = Math.Round(float.Parse(shijichukuzhongliang) - float.Parse(shijichukuzhongliang) / sumToatle * float.Parse(zhongliang_shengyu),3) ;
                            }

                        }
                        i++;

                    }


                }
            }
        }

        private void fuliao_exportExcel_Click(object sender, EventArgs e)
        {
            if (!"99991231".Equals(fuliaoLastJijiDate.ToString("yyyyMMdd")) && fuliaoLastJijiDate != this.fuliao_startday.Value)
            {
                DateTime dt = fuliaoLastJijiDate.AddDays(-1);
                if (MessageBox.Show("上次集计时间截止为" + dt.Year + "年" + dt.Month + "月" + dt.Day + "日,如果继续集计可能造成数据不完整或数据丢失！", "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
            }

            //剩余重量检验
            int i = 0;
            int errorNum = 0;
            string errorMessage = "";
            foreach (DataGridViewRow row in this.dataGridView_fuliao.Rows)
            {
                if (row.Cells[7].Value != null)
                {
                    if (row.Cells[0].Value == null)
                    {
                        if (!COMMAND.Command.isFormatByReg(row.Cells[7].Value.ToString(), "^(-?[0-9])+(.[0-9]{1,3})?$"))
                        {
                            this.dataGridView_fuliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                            // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            errorMessage = "实际使用重量总量输入有误！";
                            errorNum++;
                        }
                        i++;
                        continue;
                    }
                    string zhongliang_shiji = row.Cells[7].Value.ToString();
                    if (!COMMAND.Command.isFormatByReg(zhongliang_shiji, "^(-?[0-9])+(.[0-9]{1,3})?$"))
                    {
                        this.dataGridView_fuliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        // MessageBox.Show("剩余重量输入有误！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量输入有误！";
                        errorNum++;
                    }
                        /*
                    else if (float.Parse(row.Cells[7].Value.ToString()) > float.Parse(row.Cells[6].Value.ToString()))
                    {
                        this.dataGridView_fuliao.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        //MessageBox.Show("剩余重量不能大于重量(集计钱)！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        errorMessage = "实际使用重量不能大于实际出库重量！";

                        errorNum++;
                    }*/
                }

                i++;
            }

            if (errorNum > 0)
            {
                MessageBox.Show(errorMessage, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //其他输入检验
            string errorCheckMesg = inputCheckFuLiao();

            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int numcont = this.dataGridView_fuliao.Rows.Count;
            Selectresult = new object[numcont, 9];

            int num = 0;
            int resultRow = 0;
            foreach (DataGridViewRow row in this.dataGridView_fuliao.Rows)
            {
                if (row.Cells[0].Value != null && !"".Equals(row.Cells[0].Value.ToString()))
                {

                    //品名
                    Selectresult[resultRow, 0] = row.Cells[0].Value;
                    //规格
                    Selectresult[resultRow, 1] = row.Cells[1].Value;
                    //备注
                    Selectresult[resultRow, 2] = row.Cells[2].Value;
                    //单位
                    Selectresult[resultRow, 3] = row.Cells[3].Value;
                    //实际出库重量
                    Selectresult[resultRow, 4] = row.Cells[6].Value;
                    //剩余
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        Selectresult[resultRow, 5] = row.Cells[6].Value;
                    }
                    else
                    {
                        Selectresult[resultRow, 5] = row.Cells[7].Value;
                    }


                    Selectresult[resultRow, 6] = this.fuliao_startday.Value.ToString("yyyyMMdd");
                    Selectresult[resultRow, 7] = this.fuliao_endDay.Value.ToString("yyyyMMdd");
                    if (row.Cells[5].Value == null)
                    {
                        Selectresult[resultRow, 8] = "0";
                    }
                    else
                    {
                        Selectresult[resultRow, 8] = row.Cells[5].Value;
                    }
                    resultRow++;

                }
                else
                {
                    if (row.Cells[7].Value == null || "".Equals(row.Cells[7].Value.ToString()))
                    {
                        row.DefaultCellStyle.BackColor = Color.LightPink;
                        MessageBox.Show("实际出库总重量必须输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                num++;
            }

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_fuliaoBean bean = new FWMS.Bean.Chukuguanli_fuliaoBean();
            bean.Selectresult = Selectresult;
            bean.Resultrow = resultRow;
            bool chukumingsiUpdate = service.chukumingxi_fuLiaoServicUpdate(bean);
            // bool chukuguanliUpdate = service.chukuguanli_yuancailiaoServicUpdate(bean, this.dateTimePicker_startday.Value.ToString("yyyyMMdd"), this.dateTimePicker_endday.Value.ToString("yyyyMMdd"));
            //bool kuchunguanliUPdate = service.kuchunguanli_update(this.dataGridView_fuliao);


            if (!chukumingsiUpdate)
            {
                MessageBox.Show("保存失败，请重新保存！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("数据保存成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
         
        }

        private void fuliao_search_Click(object sender, EventArgs e)
        {
            //其他输入检验
            string errorCheckMesg = "";

             DateTime dateStartDate = this.fuliao_startday.Value;
            DateTime dateEndDate = this.fuliao_endDay.Value;
            if (dateStartDate == null)
            {
                errorCheckMesg =  "集计开始日期必须输入！";
            }
            else if (dateEndDate == null)
            {
                errorCheckMesg = "集计截止日期必须输入！";
            }
            int period = (dateEndDate - dateStartDate).Hours;
            if (period < 0)
            {
                errorCheckMesg = "集计开始日期不能在集计结束日期之后！";
            }
            else if (period > 30)
            {
                errorCheckMesg = "集计时间范围超出，请重新选择集计时间！";
            }

            if (!"".Equals(errorCheckMesg))
            {
                MessageBox.Show(errorCheckMesg, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Bean.Chukuguanli_yuancailiaoBean bean = (Bean.Chukuguanli_yuancailiaoBean)this.getData(2);
            if (bean != null)
            {
                datagridviewadddata(bean, 2,false);
                button11.Enabled = true;
                this.fuliao_exportExcel.Enabled = true;
            }
            else
            {
                this.fuliao_exportExcel.Enabled = false;
                button11.Enabled = true;
                MessageBox.Show("所选时间段内没有出库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dataGridView_fuliao_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (this.dataGridView_fuliao.SelectedCells[0].ColumnIndex == 9)
                {

                    DataGridViewCell shengyu_zhongliang = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[7];
                    

                    Form_chukumingxi_details frmMixingDetails = new Form_chukumingxi_details();
                    Object zhongliang_total = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[6].Value;
                    Object zhongliang_shengyu = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[7].Value;
                    Object pinming = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[0].Value;
                    Object guige = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[1].Value;
                    Object beizhu = this.dataGridView_fuliao.Rows[this.dataGridView_fuliao.SelectedCells[0].RowIndex].Cells[2].Value;
                    //string dateStartDate = this.dateTimePicker_startday.Value.ToString("yyyyMMdd");
                    //string dateEndDate = this.dateTimePicker_endday.Value.ToString("yyyyMMdd");
                    DateTimePicker dateStartDate = this.fuliao_startday;
                    DateTimePicker dateEndDate = this.fuliao_endDay;


                    Bean.Chukuguanli_fuliaoBean bean = (Bean.Chukuguanli_fuliaoBean)this.selectByPeriod(2, pinming, guige, beizhu, dateStartDate, dateEndDate);



                    frmMixingDetails.selfShow(2, bean, pinming, zhongliang_total, zhongliang_shengyu, dateStartDate, dateEndDate);

                }
            }
            catch (Exception)
            {

            }
        }


        private void dataGridView_tiaoliao_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView_tiaoliao.CurrentCell.ColumnIndex == 7)
            {
                Control cotrol = (Control)e.Control;
                cotrol.KeyPress += new KeyPressEventHandler(dataGridView_tiaoliao_KeyPress);
            }
        }

        private void dataGridView_tiaoliao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (this.dataGridView_tiaoliao.CurrentCell.ColumnIndex == 7)
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 46) && (e.KeyChar != 8) && (e.KeyChar != 13) && e.KeyChar != 45)
                    e.Handled = true;
                else
                    e.Handled = false;
            }
        }

        private void dataGridView_fuliao_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dataGridView_fuliao.CurrentCell.ColumnIndex == 7)
            {
                Control cotrol = (Control)e.Control;
                
            }
        }



   

        private void dataGridView_fuliao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 46) && (e.KeyChar != 8) && (e.KeyChar != 13) && e.KeyChar != 45)
                e.Handled = true;
            else
                e.Handled = false;
        }


        /**
         * 
         * 按时间段查询按钮按下事件
         * 
         * */
        private void btn_selectByPeriod_Click(object sender, EventArgs e)
        {
            COMMAND.CommandBean.cailiaostyle = 0;
            MingxiSelectByPeriod selectByPeriodForm = new MingxiSelectByPeriod();
            selectByPeriodForm.ShowDialog();

            if (COMMAND.CommandBean.jijidate == null)
            {
                return;
            }

            //集计开始结束日期赋值
            int startYear = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(0, 4));
            int startMonth = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(4, 2));
            int startDay = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(6, 2));
            int endYear = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(0, 4));
            int endMonth = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(4, 2));
            int endDay = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(6, 2));
            DateTime startDayDT = new DateTime(startYear, startMonth, startDay);
            DateTime endDayDT = new DateTime(endYear, endMonth, endDay);
            dateTimePicker_startday.Value = startDayDT;
            dateTimePicker_endday.Value = endDayDT;

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();

            bean = service.mingxi_ServiceSelectByJijiDate(dateTimePicker_startday.Value.ToString("yyyyMMdd"),this.dateTimePicker_endday.Value.ToString("yyyyMMdd"), "0");

            if (bean != null)
            {
                button9.Enabled = true;
                datagridviewadddata(bean, 0,true);
            }

            COMMAND.CommandBean.jijidate = null;
            COMMAND.CommandBean.jijiStartDay = null;
            COMMAND.CommandBean.jijiEndDay = null;
            

        }

        private void button5_Click(object sender, EventArgs e)
        {
            MingxiSelectByPeriod selectByPeriodForm = new MingxiSelectByPeriod();
            selectByPeriodForm.ShowDialog();

            if (COMMAND.CommandBean.jijidate == null)
            {
                return;
            }

            //集计开始结束日期赋值
            int startYear = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(0, 4));
            int startMonth = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(4, 2));
            int startDay = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(6, 2));
            int endYear = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(0, 4));
            int endMonth = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(4, 2));
            int endDay = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(6, 2));
            DateTime startDayDT = new DateTime(startYear, startMonth, startDay);
            DateTime endDayDT = new DateTime(endYear, endMonth, endDay);
            tiaoliao_startday.Value = startDayDT;
            tiaoliao_endday.Value = endDayDT;

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();

            bean = service.mingxi_ServiceSelectByJijiDate(tiaoliao_startday.Value.ToString("yyyyMMdd"),this.tiaoliao_endday.Value.ToString("yyyyMMdd"), "1");

            if (bean != null)
            {
                button10.Enabled = true;
                datagridviewadddata(bean, 1, true);
            }
         

            COMMAND.CommandBean.jijidate = null;
            COMMAND.CommandBean.jijiStartDay = null;
            COMMAND.CommandBean.jijiEndDay = null;
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MingxiSelectByPeriod selectByPeriodForm = new MingxiSelectByPeriod();
            selectByPeriodForm.ShowDialog();

            if (COMMAND.CommandBean.jijidate == null)
            {
                return;
            }

            //集计开始结束日期赋值
            int startYear = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(0, 4));
            int startMonth = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(4, 2));
            int startDay = int.Parse(COMMAND.CommandBean.jijiStartDay.Substring(6, 2));
            int endYear = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(0, 4));
            int endMonth = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(4, 2));
            int endDay = int.Parse(COMMAND.CommandBean.jijiEndDay.Substring(6, 2));
            DateTime startDayDT = new DateTime(startYear, startMonth, startDay);
            DateTime endDayDT = new DateTime(endYear, endMonth, endDay);
            fuliao_startday.Value = startDayDT;
            fuliao_endDay.Value = endDayDT;

            Service.ChukumingxiService service = new FWMS.Service.ChukumingxiService();
            Bean.Chukuguanli_yuancailiaoBean bean = new FWMS.Bean.Chukuguanli_yuancailiaoBean();

            bean = service.mingxi_ServiceSelectByJijiDate(fuliao_startday.Value.ToString("yyyyMMdd"), this.fuliao_endDay.Value.ToString("yyyyMMdd"), "2");

            if (bean != null)
            {
                button11.Enabled = true;
                datagridviewadddata(bean, 2, true);
            }

            COMMAND.CommandBean.jijidate = null;
            COMMAND.CommandBean.jijiStartDay = null;
            COMMAND.CommandBean.jijiEndDay = null;
            
        }



        private void button9_Click(object sender, EventArgs e)
        {
                string template = "原料出库明细MODEL.xls";
                string sheet = "原料出库明细";
                exportExcel(template,sheet, this.dataGridView_yuanliao);
        }

        private void button10_Click(object sender, EventArgs e)
        {
                string template = "调味料出库明细MODEL.xls";
                string sheet = "调味料出库明细";
                exportExcel(template, sheet, this.dataGridView_tiaoliao);

        }

        private void button11_Click(object sender, EventArgs e)
        {
                string template = "辅料出库明细MODEL.xls";
                string sheet = "辅料出库明细";
                exportExcel(template, sheet, this.dataGridView_fuliao);
        }


        private string getLastJijiDate(string type)
        {
            Bean.Chukumingxi_yuancailiaoBean bean = new FWMS.Bean.Chukumingxi_yuancailiaoBean();
            Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();

            return ChukumingxiService.mingxiJiJiLastDate(type);
        }

        private string getLastJijiDateStart(string type)
        {
            Bean.Chukumingxi_yuancailiaoBean bean = new FWMS.Bean.Chukumingxi_yuancailiaoBean();
            Service.ChukumingxiService ChukumingxiService = new FWMS.Service.ChukumingxiService();

            return ChukumingxiService.mingxiJiJiLastDateStart(type);
        }

        private void dataGridView_tiaoliao_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            lbl_count.Visible = true;
            double sum = 0;
            bool isVisible = true;
            foreach (DataGridViewCell cell in dataGridView_tiaoliao.SelectedCells)
            {
                if (cell.Value != null)
                {
                    if (Command.IsFloat(cell.Value.ToString()))
                    {
                        sum += Math.Round(float.Parse(cell.Value.ToString()), 3);
                    }
                    else
                    {
                        isVisible = false;
                    }
                }
                else
                {
                    isVisible = false;
                }
            }
            lbl_count.Text = "求和=" + Convert.ToString(sum);
            if (!isVisible)
            {
                lbl_count.Visible = false;
            }
           
        }

        private void dataGridView_yuanliao_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            lbl_yuanliaosum.Visible = true;
            double sum = 0;
            bool isVisible = true;
            foreach (DataGridViewCell cell in this.dataGridView_yuanliao.SelectedCells)
            {
                if (cell.Value != null)
                {
                    if (Command.IsFloat(cell.Value.ToString()))
                    {
                        sum += Math.Round(float.Parse(cell.Value.ToString()), 3);
                    }
                    else
                    {
                        isVisible = false;
                    }
                }
                else
                {
                    isVisible = false;
                }
            }
            lbl_yuanliaosum.Text = "求和=" + Convert.ToString(sum);
            if (!isVisible)
            {
                lbl_yuanliaosum.Visible = false;
            }
        }

        private void dataGridView_fuliao_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            lbl_fuliaosum.Visible = true;
            double sum = 0;
            bool isVisible = true;
            foreach (DataGridViewCell cell in this.dataGridView_fuliao.SelectedCells)
            {
                if (cell.Value != null)
                {
                    if (Command.IsFloat(cell.Value.ToString()))
                    {
                        sum += Math.Round(float.Parse(cell.Value.ToString()), 3);
                    }
                    else
                    {
                        isVisible = false;
                    }
                }
                else
                {
                    isVisible = false;
                }
            }
            lbl_fuliaosum.Text = "求和=" + Convert.ToString(sum);
            if (!isVisible)
            {
                lbl_fuliaosum.Visible = false;
            }
        }


        
}
}
